package fashionshop.dal;

import java.sql.ResultSet;

import database.MySQLConnect;
import fashionshop.bll.CategoryBLL;

public class CategoryDAL {
	
	public CategoryDAL(){
	}
	
	public static ResultSet LoadAll() throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sqlQuery = "SELECT * FROM category";
		return conn.executeQuery(sqlQuery);
	}
	
	public static ResultSet LoadByID(int id) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "SELECT * FROM category WHERE ID = '%d'";
		String sqlQuery = String.format(sql, id);
		return conn.executeQuery(sqlQuery);
	}
	
	public static ResultSet LoadIDByName(String name) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "SELECT ID FROM category WHERE Name = '%s'";
		String sqlQuery = String.format(sql, name);
		return conn.executeQuery(sqlQuery);
	}

	public static int Insert(CategoryBLL category) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "INSERT INTO category (`Name`, `Active`) VALUES ('%s', '%d')";
		String sqlQuery = String.format(sql, category.getName(), category.getActive());
		return conn.executeUpdate(sqlQuery);
	}
	
	public static int Update(CategoryBLL category) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "UPDATE category SET `Name`='%s', `Active`='%d' WHERE `ID`='%d'";
		String sqlQuery = String.format(sql, category.getName(), category.getActive(), category.getId());
		return conn.executeUpdate(sqlQuery);
	}
	
//	public static int Delete(CategoryBLL category) throws Exception{
//		MySQLConnect conn = new MySQLConnect();
//		String sql = "UPDATE category SET `Active`='0' WHERE `ID`='%d'";
//		String sqlQuery = String.format(sql, category.getId());
//		return conn.executeUpdate(sqlQuery);
//	}
//	
//	public static int Delete(int id) throws Exception{
//		MySQLConnect conn = new MySQLConnect();
//		String sql = "UPDATE category SET `Active`='0' WHERE `ID`='%d'";
//		String sqlQuery = String.format(sql, id);
//		return conn.executeUpdate(sqlQuery);
//	}
}
